package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
)



func write_demo1(){
	// 打开文件写内容
	fileObj, err := os.OpenFile("./xx.txt", os.O_WRONLY | os.O_APPEND | os.O_CREATE | os.O_TRUNC, 0644)
	if err != nil {
		fmt.Printf("open file failed, err: %v\n", err)
		return
	}
	// write
	fileObj.Write([]byte("zhouzhou i hei you\n"))
	// writeString
	fileObj.WriteString(("周周我黑泥"))
	fileObj.Close()
}

func write_demo2(){
	fileObj, err := os.OpenFile("./xx.txt", os.O_WRONLY | os.O_APPEND | os.O_CREATE | os.O_TRUNC, 0644)
	if err != nil {
		fmt.Printf("open file failed, err: %v\n", err)
		return
	}
	defer fileObj.Close()
	// 创建一个写的对象
	wr := bufio.NewWriter(fileObj)
	wr.WriteString("hello沙河\n")	// 写到缓存中
	wr.Flush()	// 将缓存中的内存写入文件
}

func writeDemo3(){
	str := "hello 呵呵呵"
	err := ioutil.WriteFile("./xx.txt",[]byte(str), 0666)
	if err != nil{
		fmt.Printf("write file failed, err: %v\n", err)
		return
	}
}

func main() {
	//write_demo2()
	writeDemo3()
}
